package org.liukai.servlet_ems.filter;



import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.FilterChain;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @create LoginCheckFilter.java 15:04
 * 
 * @author liukai
 * @date 2015/12/17
 */
public class LoginCheckFilter implements Filter {



  

	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	public void destroy() {
		// TODO Auto-generated method stub
		
	}
	
	//登录校验filter
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {
		System.out.println("__________________________________________________________________________");
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        String urlString = request.getRequestURI();
        HttpSession session = request.getSession();
        //排除过滤链接
        if(urlString.endsWith("/user/toLogin.action") || urlString.endsWith("/user/login.action") || urlString.endsWith("/error/error.action")) {
            filterChain.doFilter(request, response);
        } else {
            //判断当前session中是否有登录记号
            Object currentUser = session.getAttribute("currentUser");
            if(null == currentUser) {
                request.setAttribute("msg", "请先登录");
                request.getRequestDispatcher("/WEB-INF/user/login.jsp").forward(request, response);
            } else {
                filterChain.doFilter(request, response);
            }
        }
		
	}


}
